
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../base_widget/base_scaffold.dart';

class WebviewScreen extends StatefulWidget {
  final String url;

  const WebviewScreen({super.key, required this.url});

  @override
  State<WebviewScreen> createState() => _WebviewScreenState();
}

class _WebviewScreenState extends State<WebviewScreen> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  final Completer<WebViewController> _controller =
  Completer<WebViewController>();
  String title = "网页";
  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      title: title,
      body: WebView(
        initialUrl: widget.url,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _controller.complete(webViewController);
        },
        // navigationDelegate: (NavigationRequest request) {
        //   if (request.url.startsWith('https://www.example.com')) {
        //     // 处理加载中，如显示进度条
        //     return NavigationDecision.navigate;
        //   }
        //   // 处理其他情况，如打开其他应用或链接
        //   return NavigationDecision.prevent;
        // },
        onPageFinished: (String url) async {
          if (_controller.isCompleted) {
            title = (await (await _controller.future).getTitle())!;
            setState(() {

            });
          }
        },
      ),
    );
  }
}


// import 'dart:async';
//
// import 'package:flutter/cupertino.dart';
// import 'package:jetour_line/base_widget/base_scaffold.dart';
// import 'package:webview_flutter/webview_flutter.dart';
//
// class WebviewScreen extends StatefulWidget {
//   final String url;
//
//   const WebviewScreen({super.key, required this.url});
//
//   @override
//   State<WebviewScreen> createState() => _WebviewScreenState();
// }
//
// class _WebviewScreenState extends State<WebviewScreen> {
//   late WebViewController controller;
//
//   @override
//   void initState() {
//     // TODO: implement initState
//     super.initState();
//     _initWebViewController();
//
//   }
//
//   String title = "网页";
//
//   @override
//   Widget build(BuildContext context) {
//
//
//     return BaseScaffold(
//       title: title,
//       body: WebViewWidget(controller: controller),
//
//     );
//   }
//
//
//   void _initWebViewController() {
//     controller = WebViewController()
//       ..setJavaScriptMode(JavaScriptMode.unrestricted)
//       ..setBackgroundColor(const Color(0x00000000))
//       ..setNavigationDelegate(
//         NavigationDelegate(
//           onProgress: (int progress) {
//             // Update loading bar.
//           },
//           onPageStarted: (String url) {},
//           onPageFinished: (String url) async {
//             title = await controller.getTitle()??'';
//             setState(() {});
//           },
//           onWebResourceError: (WebResourceError error) {},
//           onNavigationRequest: (NavigationRequest request) {
//             return NavigationDecision.navigate;
//           },
//         ),
//       )
//       ..loadRequest(Uri.parse(widget.url));
//   }
//
// }
